package com.joe.gulishop.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.joe.common.valid.AddGroup;
import com.joe.common.valid.UpdateGroup;
import com.joe.common.valid.UpdateStatusGroup;
import com.joe.gulishop.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joe.gulishop.product.entity.BrandEntity;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.R;

import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;


/**
 * 品牌
 *
 * @author joe
 * @email joe@gmail.com
 * @date 2021-12-03 10:45:56
 */

@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存 @Valid开启的校验功能
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand/*,BindingResult result*/){
       /* if (result.hasErrors()){
//            获取所有的校验的结果
            Map<String,String> map=new HashMap<>();
            result.getFieldErrors().forEach((item)->{
//                设置所有的校验错误结果循环取出
                String defaultMessage = item.getDefaultMessage();
                //获取错误的属性的名字
                String field = item.getField();
                map.put(field,defaultMessage);

            });
            return   R.error(400,"提交的数据不合法").put("data",map);
        }else {*/
            brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand/*, BindingResult result*/){
     /*   if (result.hasErrors()){
//            获取所有的校验的结果
            Map<String,String> map=new HashMap<>();
            result.getFieldErrors().forEach((item)->{
//                设置所有的校验错误结果循环取出
                String defaultMessage = item.getDefaultMessage();
                //获取错误的属性的名字
                String field = item.getField();
                map.put(field,defaultMessage);

            });
          return   R.error(400,"提交的数据不合法").put("data",map);
        }else {*/
            brandService.updateById(brand);

        return R.ok();
    }
/*
*   更新品牌状态
* */
    @RequestMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class)@RequestBody BrandEntity brand) {
        System.out.println(brand.getShowStatus());
        UpdateWrapper<BrandEntity> wrapper=new UpdateWrapper();
        wrapper.eq("brand_id",brand.getBrandId());
        wrapper.set("show_status",brand.getShowStatus());
        brandService.update(wrapper);
//
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
